let User = require('../../database/UserSchema')
let captchatwo = require('../user-routes/captcha')
module.exports = async (req, res) => {
	let captcha = req.body.captcha.toLowerCase()
	if (captcha != tempCaptcha) {
		res.send(
			`<script>
			if(confirm("验证码错误")){
				window.location.href="http://localhost:3000/admin/login"
			}
		</script>`
		)
	} else {
		let namedata = await User.findOne({
			"username": req.body.name
		})
		if (!namedata) {
			res.send(
				`<script>
				if(confirm("该用户不存在")){
					window.location.href="http://localhost:3000/admin/login"
				}
			</script>`
			)
		} else {
			let userinfo = {
				username: req.body.name,
				password: req.body.password
			}
			let finallynew = await User.find(userinfo)
			if (finallynew.length == 0) {
				// res.send('密码错误')
				res.send(
					`<script>
					if(confirm("密码错误")){
						window.location.href="http://localhost:3000/admin/login"
					}
				</script>`
				)
			} else {
				req.app.locals.username = req.body.name
				res.redirect('/admin/main')
			}
		}
	}
}
